##=============================================================================
## Appendix Figure 20
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "lme4", "sjPlot")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

# Frontline oblasts only
dat <- dat %>% 
  filter(frontline_b == 1) # ~25% of respondents live along the frontline

fit <- glmer(compromise ~ 1 + wave + survival_mindset + per_idx_invcov + 
                fam_idx_invcov + ln_viina_two_months +
                displaced + sex + rus_ethnic + age + edu + job + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                   adm1_en), 
              data = dat, 
              family = binomial,
              control = glmerControl(optimizer = "bobyqa"))

tab_model(fit)
AIC(logLik(fit))

plot_model(fit, 
           sort.est = TRUE, 
           vline.color = "red",
           show.values = TRUE, 
           value.offset = .3,
           axis.labels = "", 
           title = "Effect of Survival Mindset on Support for Saving Lives (Frontline communities only)")

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================